package com.ctg.behavior.calc.controller.events;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.common.CtgResult;
import com.ctg.behavior.common.event.CtEventGroupListResult;
import com.ctg.behavior.common.event.CtEventGroupsResult;
import com.ctg.behavior.common.event.CtEventListResult;
import com.ctg.behavior.common.properties.CtPropertiesResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.InputStream;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/meta")
public class MetaEventController {

	@Value("${simulator.path}")
	private String simlutorPath;

	@PostMapping("/event/group/list")
	public CtEventGroupsResult groupsList1() throws Exception {
		CtEventGroupsResult result = new CtEventGroupsResult();
		log.info("simlutorPath={}", simlutorPath);

		String filePath = simlutorPath + "/events/event.group.list.json";
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			// File resourceFile = ResourceUtils.getFile("classpath:all.events.json");
			ClassPathResource classPathResource = new ClassPathResource("event/report/event.group.list.json");
			InputStream inputStream = classPathResource.getInputStream();
			List<String> lines = IOUtils.readLines(inputStream, "UTF-8");
			StringBuilder sbuilder = new StringBuilder();
			for (String line : lines) {
				sbuilder.append(line);
			}
			result = JSON.parseObject(sbuilder.toString(), CtEventGroupsResult.class);
		} else {
			String resultText = FileUtils.readFileToString(targetFile, "UTF-8");
			result = JSON.parseObject(resultText, CtEventGroupsResult.class);
		}

		return result;
	}

	@PostMapping("/events/groups/list")
	public CtEventGroupsResult groupsList() throws Exception {

		CtEventGroupsResult result = new CtEventGroupsResult();
		log.info("simlutorPath={}", simlutorPath);

		String filePath = simlutorPath + "/events/event.group.list.json";
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			// File resourceFile = ResourceUtils.getFile("classpath:all.events.json");
			ClassPathResource classPathResource = new ClassPathResource("event/report/event.group.list.json");
			InputStream inputStream = classPathResource.getInputStream();
			List<String> lines = IOUtils.readLines(inputStream, "UTF-8");
			StringBuilder sbuilder = new StringBuilder();
			for (String line : lines) {
				sbuilder.append(line);
			}
			result = JSON.parseObject(sbuilder.toString(), CtEventGroupsResult.class);
		} else {
			String resultText = FileUtils.readFileToString(targetFile, "UTF-8");
			result = JSON.parseObject(resultText, CtEventGroupsResult.class);
		}

		return result;
	}

	@PostMapping("/events/groups/listTMU")
	public CtgResult groupsListTMU(@RequestBody CtEventGroupListResult tmResult) throws Exception {

		CtgResult result = new CtgResult();
		log.info("tmResult={}", tmResult);

		String filePath = simlutorPath + "/events";
		FileUtils.forceMkdir(new File(filePath));
		String targetFilePath = filePath + "/event/report/event.group.list.json";
		File targetFile = new File(targetFilePath);
		FileUtils.writeStringToFile(targetFile, JSON.toJSONString(tmResult), "UTF-8");

		return result;
	}

}
